package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.helper.R;
import cn.edu.ctbu.labtest.helper.RUtil;
import cn.edu.ctbu.labtest.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/findbyname")
    public R<Teacher> findByName(String name, String order, Integer limit, Integer offset){
        Page<Teacher> all;
        Pageable pageable = PageRequest.of(offset/limit,limit,
                Sort.by("id").descending());

        if(name==null||name.equals("")){
            all=teacherService.findAll(pageable);
        }
        else{
            all=teacherService.findByNameLike("%"+name+"%",pageable);
        }
        return RUtil.success(all);
    }

    /**
     * 读取的url:/webapi/teacher/getByPage
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getByPage")
    public PageUtils getByPage(@RequestParam(value ="page",defaultValue = "0")Integer page,
                               @RequestParam(value = "size",defaultValue = "10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name)
    {

        //按id排倒序
        Sort sort=Sort.by("id").descending();
        Page<Teacher> teacherPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            teacherPage=teacherService.findAll(pageable);
        }
        else{
            Teacher teacher=new Teacher();
            teacher.setName(name);

            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询,即%{tname}%
            Example<Teacher> example=Example.of(teacher,matcher);
            teacherPage=teacherService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return  pageUtils;
    }

    /**
     * 读取的url:/webapi/teacher/tlist
     * @return
     */
    @GetMapping("/tlist")
    public List<Teacher> getAll(){
        List<Teacher> teachers=teacherService.findAll();
        return teachers;
    }

    /**
     * 读取的url:/webapi/teacher/findByName
     */
    @GetMapping("/findByName")
    public  List<Teacher> findByName(String name){
        List<Teacher> all;
        if(StringUtils.isEmpty(name)){
            all=teacherService.findAll();
        }
        else{
            all=teacherService.findByName(name);
        }
        return all;
    }

    @GetMapping("/findByNameLike")
    public  Page<Teacher> findByNameLike(String name,Pageable pageable){
        Page<Teacher> all;
        if(StringUtils.isEmpty(name)){
            all=teacherService.findAll(pageable);
        }
        else{
            all=teacherService.findByNameLike(name,pageable);
        }
        return all;
    }

    /**
     * 读取的url:/webapi/teacher/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable Integer id){
        Teacher teachers=teacherService.getById(id);
        teachers.setPassword("");
        return teachers;
    }

//    /**
//     * 新增教师，/webapi/teacher/insert
//     */
//    @PostMapping("/insert")
//    public Teacher insert(Teacher teacher){
//        Teacher teacher1=teacherService.insert(teacher);
//        return teacher1;
//    }
//
//    /**
//     * 更新教师，/webapi/teacher/updata
//     */
//    @PutMapping("/update")
//    public Teacher update(Teacher teacher){
//        Teacher teacher1=teacherService.insert(teacher);
//        return teacher1;
//    }

}
